package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.project.system.domain.SysModuleNotice;
import com.dhecp.project.system.service.SysModuleNoticeService;

/**
 * 通知 控制层
 * @author DH-Xxj
 * @version 2020年8月17日
 */
@Controller
@RequestMapping("/system/modulenotice")
public class SysModuleNoticeController extends BaseController{

	private String moduleTable = ModuleTable.MODULE_TASK;
	
	@Autowired
    private SysModuleNoticeService moduleNoticeService;
	
	@Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
	
	/**
     * 查询生成通知表格gridColumns
     * @param params
     * @return
     */
    @GetMapping("/grid")
    @ResponseBody
    public Map<String, Object> selectGrid(@RequestParam Map<String, Object> params){
    	Map<String, Object> dicFieldsList = new HashMap<String, Object>();
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysModuleNotice()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 150);
    	params.put("checkBox", "yes");
    	dicFieldsList=gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
		return dicFieldsList;
    }
	
	/**
	 * 查询未读通知数
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listwds")
    @ResponseBody
    public TableDataInfo listWds(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        Integer noticeCount = 0;	//通知数
        List<Map<String, Object>> noticeCountList =  new ArrayList<Map<String, Object>>();
        try {
        	noticeCount = moduleNoticeService.queryModuleNoticeWds(params);
        	Map<String, Object> noticeCountMap = new HashMap<String, Object>();
        	noticeCountMap.put("noticeCount", noticeCount);
        	noticeCountList.add(noticeCountMap);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(noticeCountList);
    }
	
	/**
	 * 查询未读通知
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listwd")
    @ResponseBody
    public TableDataInfo listWd(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleNotice> moduleTaskList = new ArrayList<SysModuleNotice>();
        try {
        	moduleTaskList = moduleNoticeService.queryModuleNoticeWd(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
	
	/**
	 * 查询已读通知
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listyd")
    @ResponseBody
    public TableDataInfo listYd(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleNotice> moduleTaskList = new ArrayList<SysModuleNotice>();
        try {
        	moduleTaskList = moduleNoticeService.queryModuleNoticeYd(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
	
	/**
	 * 查询当前用户为发送人数据
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listfs")
    @ResponseBody
    public TableDataInfo listFs(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleNotice> moduleTaskList = new ArrayList<SysModuleNotice>();
        try {
        	moduleTaskList = moduleNoticeService.queryModuleNoticeFs(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
	
	/**
	 * 通知标记为已读
	 * @param infoItems 对应实体类的json格式字符串（仅需 guid）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @PostMapping("/toreadpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult taskStateToReadPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("infoItems") == null || params.get("infoItems") == ""){
        	return AjaxResult.error("缺少参数infoItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= moduleNoticeService.updateTaskStateToRead(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 新增通知
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @PostMapping("/addModuleNotice")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult moduleNoticeAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= moduleNoticeService.insertModuleNotice(params);
			
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
	
}
